package com.atguigu.yygh.hosp.util;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName hospApiUtil
 * @Description: 类描述
 * @Author: admin
 * @CreateDate: 2024/6/4 17:51
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/6/4 17:51
 * @UpdateRemark: 更新的信息
 * @Version: 1.0
 */


public class HospApiUtil {
    /**
     * 接收医院接口数据并进行签名验证
     * @param request 将传过来的数据进行处理
     * @param hospitalSetService 用于查询签名密钥
     * @return 返回处理好的参数
     */
    public static Map<String, Object> hospStringToObject(HttpServletRequest request, HospitalSetService hospitalSetService){
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramsMap = HttpRequestHelper.switchMap(requestMap);

        String hoscode = (String) paramsMap.get("hoscode");

        //获取医院传递过来的签名
        String hospSign = (String) paramsMap.get("sign");

        //根据医院编号来查询对应签名
        String signKey=hospitalSetService.getSignKey(hoscode);

        //将查询数据也进行md5加密
        String signKeyMd5 = MD5.encrypt(signKey);

        //判断签名是否正确
        if (!hospSign.equals(signKeyMd5)) {
            //错误抛出异常
            throw  new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        return paramsMap;
    }
}
